package com.wannengbang.cloudleader.service;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.bean.PlanDetailsListBean;
import com.wannengbang.cloudleader.event.PlanEvent;
import com.wannengbang.cloudleader.service.adapter.PlanDetailsListAdapter;
import com.wannengbang.cloudleader.service.model.IServiceModel;
import com.wannengbang.cloudleader.service.model.ServiceModelImpl;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.CommonNoTitleDialog;
import com.wannengbang.cloudleader.widget.ViewLoading;
import com.wannengbang.cloudleader.widget.WrapContentLinearLayoutManager;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PlanDetailsActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_repayment_amount)
    TextView tvRepaymentAmount;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_repayment_count)
    TextView tvRepaymentCount;
    @BindView(R.id.tv_repayment_completed)
    TextView tvRepaymentCompleted;
    private String plan_id;
    private IServiceModel serviceModel;
    private List<PlanDetailsListBean.DataBean.DetailBean> beanList;
    private PlanDetailsListAdapter listAdapter;
    private CommonNoTitleDialog.Builder dialogBuild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plan_details);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        plan_id = getIntent().getStringExtra("plan_id");
        serviceModel = new ServiceModelImpl();
        beanList = new ArrayList<>();
        listAdapter = new PlanDetailsListAdapter(beanList);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);
        listAdapter.setOnCallBackListener((type, status, childId) -> {
            // 1待完成 2已完成
            if (type == 1 && status == 1) {
                dialogBuild = new CommonNoTitleDialog.Builder(mActivity);
                dialogBuild.setMessage("是否确定完成此消费");
                dialogBuild.setPositiveVersionButton("确定", v -> {
                    dialogBuild.dismiss();
                    dialogBuild = null;
                    requestPlanComplete(childId + "");
                });
                dialogBuild.setNegativeButton("取消", v -> {
                    dialogBuild.dismiss();
                    dialogBuild = null;
                });
                dialogBuild.setCancelable(false);
                dialogBuild.show();
            }
            if (type == 2 && status == 1) {
                dialogBuild = new CommonNoTitleDialog.Builder(mActivity);
                dialogBuild.setMessage("是否确定完成此还款");
                dialogBuild.setPositiveVersionButton("确定", v -> {
                    dialogBuild.dismiss();
                    dialogBuild = null;
                    requestRepaymentComplete(childId + "");
                });
                dialogBuild.setNegativeButton("取消", v -> {
                    dialogBuild.dismiss();
                    dialogBuild = null;
                });
                dialogBuild.setCancelable(false);
                dialogBuild.show();
            }
        });
        requestPlanDetails();
    }

    public void requestPlanDetails() {
        ViewLoading.showProgress(mActivity, "加载中......");
        serviceModel.requestPlanDetails(plan_id, new DataCallBack<PlanDetailsListBean>() {
            @Override
            public void onSuccessful(PlanDetailsListBean bean) {
                beanList.clear();
                if (bean.getData().getDetail() != null && bean.getData().getDetail().size() > 0) {
                    beanList.addAll(bean.getData().getDetail());
                    listAdapter.notifyDataSetChanged();
                    tvRepaymentCount.setText("计划还款天数：" + beanList.size() + "天");
                    tvDate.setText("计划日期：" + bean.getData().getStart_date() + " ~ " + bean.getData().getEnd_date());
                    String totalMoney = "0";
                    for (PlanDetailsListBean.DataBean.DetailBean dataBean : beanList) {
                        totalMoney = NumberFormatUtils.formatDouble(Double.parseDouble(totalMoney) + Double.parseDouble(dataBean.getRepayment_amount()) / 100);
                    }
                    tvRepaymentAmount.setText(totalMoney);
                    String completed = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getCompleted()) / 100);
                    String todo = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getTodo()) / 100);
                    tvRepaymentCompleted.setText("还款金额已完成：" + completed + "元，未完成：" + todo + "元");
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void requestPlanComplete(String childId) {
        serviceModel.requestPlanComplete(childId, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                requestPlanDetails();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void requestRepaymentComplete(String childId) {
        serviceModel.requestPlanRepaymentComplete(childId, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                EventBus.getDefault().post(new PlanEvent());
                requestPlanDetails();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }
}
